package com.rockcent.mall.lib.helper;

import com.rockcent.common.common.BusinessException;
import com.rockcent.common.domain.enumclass.Whether;
import com.rockcent.mall.lib.domain.MallOrder;
import com.rockcent.mall.lib.domain.MarketOrder;
import com.rockcent.mall.lib.domain.MerchantOrder;
import com.rockcent.mall.lib.domain.enumclass.*;

import java.util.List;


/**
 * Created by oyhk on 2016/12/5.
 *
 * 平台订单状态机制
 */
public enum OrderStatusHelper {
    SINGLETON;

    /**
     * 更改商家订单状态
     * @param mallOrder 平台订单
     * @param merchantOrderList 商家订单列表
     */
    private void updateMerchantOrderStatus(MallOrder mallOrder, List<MerchantOrder> merchantOrderList, OrderStatus status){
        for (MerchantOrder merchantOrder : merchantOrderList) {
            if (merchantOrder.getRefundable() == Whether.NO
                    && (mallOrder.getStatus() == MallOrderStatus.PAYED
                    || mallOrder.getStatus() == MallOrderStatus.FINISHED
                    || mallOrder.getStatus() == MallOrderStatus.PART_FINISHED
                    || mallOrder.getStatus() == MallOrderStatus.FULL_REFUND)) {
                throw new BusinessException(String.format("平台商家订单包含 不可退产品，此订单不能取消"));
            }

            if (merchantOrder.getStatus() != OrderStatus.WAIT_PAYING
                    && merchantOrder.getStatus() != OrderStatus.WAIT_USE
                    && merchantOrder.getStatus() != OrderStatus.WAIT_STOCK
                    && merchantOrder.getStatus() != OrderStatus.WAIT_SHIPPED
                    && merchantOrder.getStatus() != OrderStatus.WAIT_SHOP_PICKUP) {
                throw new BusinessException(String.format("平台商家订单包含 不能退款状态，此订单不能取消"));
            }
            merchantOrder.setStatus(status);
            merchantOrder.setRefundType(RefundType.REFUNDED);
        }
    }

    /**
     * 用户发起退款申请
     * @param mallOrder 平台订单
     * @param merchantOrderList 商家订单列表
     */
    public void mallOrderRefund(MallOrder mallOrder, List<MerchantOrder> merchantOrderList){
        if (mallOrder.getStatus() != MallOrderStatus.PAYED) {
            throw new BusinessException(String.format("平台订单状态为 : %s , 不能退款", mallOrder.getStatus().toString()));
        }
        mallOrder.setStatus(MallOrderStatus.APPLY_REJECT);
        mallOrder.setRefundType(RefundType.REFUNDED);
        this.updateMerchantOrderStatus(mallOrder, merchantOrderList, OrderStatus.APPLY_REJECT);
    }

    /**
     * 平台订单取消
     * @param mallOrder 平台订单
     * @param merchantOrderList 商家订单列表
     */
    public void mallOrderCancel(MallOrder mallOrder, List<MerchantOrder> merchantOrderList) {
        if (mallOrder.getStatus() != MallOrderStatus.WAIT_PAYING && mallOrder.getStatus() != MallOrderStatus.PAYED) {
            throw new BusinessException(String.format("平台订单状态为 : %s , 不能取消订单", mallOrder.getStatus().toString()));
        }
        mallOrder.setStatus(MallOrderStatus.CANCEL);
        mallOrder.setRefundType(RefundType.REFUNDED);

        this.updateMerchantOrderStatus(mallOrder, merchantOrderList, OrderStatus.CANCEL);
    }

    /**
     * 修改平台订单为已支付
     * @param mallOrder
     */
    public void mallOrderPayed(MallOrder mallOrder) {
        if (mallOrder.getStatus() != MallOrderStatus.WAIT_PAYING) {
            throw new BusinessException(String.format("平台订单状态为 : %s , 不能支付订单", mallOrder.getStatus().toString()));
        }
        mallOrder.setStatus(MallOrderStatus.PAYED);
    }

    /**
     * 修改商家订单为已支付后订单状态
     * @param merchantOrderList
     */
    public void merchantOrderPayed(List<MerchantOrder> merchantOrderList) {
        for (MerchantOrder merchantOrder : merchantOrderList) {
            if (merchantOrder.getStatus() != OrderStatus.WAIT_PAYING) {
                throw new BusinessException(String.format("商家订单号 : %s , 状态为 : %s,不能支付订单", merchantOrder.getOrderNum(), merchantOrder.getStatus().toString()));
            }
            // 到店 && 实物
            if (merchantOrder.getProductShoppingType() == ProductShippingType.SHOP_PICKUP && merchantOrder.getOrderType() == OrderType.GOODS) {
                merchantOrder.setStatus(OrderStatus.WAIT_STOCK);
            }
            // 到店 && 服务
            if (merchantOrder.getProductShoppingType() == ProductShippingType.SHOP_PICKUP && merchantOrder.getOrderType() == OrderType.SERVICE) {
                merchantOrder.setStatus(OrderStatus.WAIT_USE);
            }
            // 物流
            if (merchantOrder.getProductShoppingType() == ProductShippingType.SHIPPING) {
                merchantOrder.setStatus(OrderStatus.WAIT_SHIPPED);
            }
        }
    }

    /**
     * 修改分销市场订单为已支付后订单状态
     * @param marketOrder
     * @param merchantOrder
     */
    public void marketOrderPayed(MarketOrder marketOrder, MerchantOrder merchantOrder) {
        if (marketOrder.getStatus() != OrderStatus.WAIT_PAYING) {
            throw new BusinessException(String.format("商家订单号 : %s , 状态为 : %s,不能支付订单", marketOrder.getOrderNum(), marketOrder.getStatus().toString()));
        }

        // 到店 && 实物
        if (merchantOrder.getProductShoppingType() == ProductShippingType.SHOP_PICKUP && merchantOrder.getOrderType() == OrderType.GOODS) {
            marketOrder.setStatus(OrderStatus.WAIT_STOCK);
        }
        // 到店 && 服务
        if (merchantOrder.getProductShoppingType() == ProductShippingType.SHOP_PICKUP && merchantOrder.getOrderType() == OrderType.SERVICE) {
            marketOrder.setStatus(OrderStatus.WAIT_USE);
        }
        // 物流
        if (merchantOrder.getProductShoppingType() == ProductShippingType.SHIPPING) {
            marketOrder.setStatus(OrderStatus.WAIT_SHIPPED);
        }
    }


}
